package com.mike.worm;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Hello world!
 */
public class App {
    static int[] random = {10,11,12,13,14,15,16,17,18,19,20};
    public static void main(String[] args) {
        String value = HttpUtils.post("http://cd.xiaozhu.com/", null, null, null, null);
        Document d1 = Jsoup.parse(value);
        Element xingzhengqus = d1.getElementById("xingzhengquS_first");
        Elements xingzhegnquss = xingzhengqus.children();
        for (Element e : xingzhegnquss) {
            try {
                String href = e.child(0).attr("href");
                String district = e.child(0).html();
//                System.out.println("标记1："+href);
                String valuexingzhengqu = HttpUtils.post(href, null, null, null, null);
                Document d2 = Jsoup.parse(valuexingzhengqu);
                Elements xiaoqulis = d2.getElementById("page_list").child(0).children();
                for (Element xiaoqulli : xiaoqulis) {
                    String xy = xiaoqulli.attr("latlng");
                    if (xiaoqulli.getElementsByClass("resule_img_a").size() > 0) {
                        String xiaoquurl = xiaoqulli.getElementsByClass("resule_img_a").get(0).attr("href");
                        String xiaoquimgtitle = xiaoqulli.getElementsByClass("resule_img_a").get(0).child(0).attr("title");
//                        System.out.println("标记2："+xiaoquurl);
                        String xiaoquhtmlStr = HttpUtils.post(xiaoquurl, null, null, null, null);
                        Document xiaoquhtml = Jsoup.parse(xiaoquhtmlStr);
                        String address = xiaoquhtml.getElementsByClass("pho_info").get(0).child(0).child(0).html();
                        String price = xiaoquhtml.getElementsByClass("day_l").get(0).child(0).html();
                        String intro = xiaoquhtml.getElementById("introduce").child(0).child(1).html();
//                        System.out.println(String.format("地址：%s,价格:%s,坐标：%s,面积：%s,区域：%s",address,price,xy,area,district));

                        String area = intro.split("<br>")[0].split("房屋面积：")[1].replace("平米","");
                        String type = intro.split("<br>")[1].split("房屋户型：")[1];
                        System.out.println(String.format("%s|%s|%s|%s|%s|%s|%s",address,price,xy,area,type,district,xiaoquurl));

                    }
                    TimeUnit.SECONDS.sleep(random[new Random().nextInt(11)]);
                }
                TimeUnit.SECONDS.sleep(random[new Random().nextInt(11)]);
            } catch (Exception error) {
                error.printStackTrace();
                System.out.println("error:");
            }

        }

    }
}
